from flask import Blueprint, render_template, request

from applications.common.curd import model_to_dicts, enable_status, disable_status
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape

from applications.models import PoliceModels
from applications.models import PoliceDetail
from applications.schemas import PoliceOutSchema
from applications.schemas import PoliceRecordSchema

police_bp = Blueprint('adminPolice', __name__, url_prefix='/admin/police')


# 管理员信息
@police_bp.get('/')
# @authorize("xxxxxxxxxxxxxxxxxx", log=True)
def police_main():
    return render_template('admin/police/main.html')

# 警员心理监测
@police_bp.get('/monitor')
# @authorize("xxxxxxxxxxxxxxxxxx", log=True)
def police_monitor():
    return render_template('admin/police/monitor.html')

#   用户分页查询1
@police_bp.get('/data')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def data():
    # 获取请求参数
    id = request.args.get('id', type=int)
    name = request.args.get('name', type=str)
    state = request.args.get('state', type=int)
    score = request.args.get('score', type=int)
    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if name:
        mf.contains(field_name="name", value=name)
    if state:
        mf.exact(field_name="state", value=state)
    if score:
        mf.exact(field_name="score",value=score)

    # orm查询
    # 使用分页获取data需要.items
    policeModels = PoliceModels.query.filter(mf.get_filter(model=PoliceModels)).layui_paginate()
    count = policeModels.total
    return table_api(data=model_to_dicts(schema=PoliceOutSchema, data=policeModels.items), count=count)


#   用户分页查询2
@police_bp.get('/data2')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def data2():
    # 获取请求参数
    police_id = request.args.get('police_id', type=int)
    class_id = request.args.get('class_id', type=int)
    score = request.args.get('score', type=int)

    # 查询参数构造
    mf = ModelFilter()
    if police_id:
        mf.exact(field_name="police_id", value=police_id)
    if class_id:
        mf.exact(field_name="class_id", value=class_id)
    if score:
        mf.exact(field_name="score",value=score)

    # orm查询
    # 使用分页获取data需要.items
    policeDetail = PoliceDetail.query.filter(mf.get_filter(model=PoliceDetail)).layui_paginate()
    count = policeDetail.total
    return table_api(data=model_to_dicts(schema=PoliceRecordSchema, data=policeDetail.items), count=count)



#  用户详情
@police_bp.get('/details')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def police_type_details():
    return render_template('admin/police/details.html')


#  用户详情
@police_bp.get('/psychological_details')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def police_type_psychological_details():

    return render_template('admin/police/psychological_details.html')